﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DAL;
using CommontLibrary;
using System.Xml;
using System.Xml.Xsl;

public partial class admin_TimKiemHocVien : System.Web.UI.UserControl
{
    #region "Member variables"
    private int intUserID = 0;
    private int intUserList = 0;
    int intChucNangID = 0;
    DALevel objLevel;
    DAQuyen objQuyen;
    DAHocVienLV2 objHocVienLV2;
    DAHocVienLV3LV5 objHocVienLV3LV5;
    DAHocVienLV6LV8 objHocVienLV6LV8;
    DAHocVien objHocVien;
    DANguoidung objNguoidung;
    DATrangThai objTrangThai;
    DATrangThaiGoi objTrangThaiGoi;
    DALog objLog;
    #endregion
    #region "Parametter variables"
    // Ham lay Bind du lieu
    public SqlConnection ConnectionView
    {
        get
        {
            object obj2 = this.ViewState["ConnectionView"];
            if (obj2 != null)
            {
                return (SqlConnection)obj2;
            }
            return null;
        }
        set
        {
            this.ViewState["ConnectionView"] = value;
        }
    }
    #endregion
    #region "Private methods"
    // Hien thi Level tren TreeView
   
    private void loadTimDanhSachHocVien()
    {
        try
        {
            string HoTen = this.txtTenHocVien.Text.Trim();
            string DienThoai = this.txtDienThoai.Text.Trim();
            DataView dv = new DataView();
            DataSet dsHocVien = objHocVien.loadTimDanhA(HoTen, DienThoai);
            if (ViewState["sortExpr"] != null)
            {
                dv = new DataView(DateTimeUntil.ConvertTable(dsHocVien.Tables[0], false));
                dv.Sort = (string)ViewState["sortExpr"];
            }
            else
            {
                dv = new DataView(DateTimeUntil.ConvertTable(dsHocVien.Tables[0], false));
            }
            this.grvHocVien.DataSource = dv;
            this.grvHocVien.DataBind();
        }
        catch
        { }
    }

    
    #endregion
    #region "Events handle"
    //Check quyen han su dung
    private void CheckUserModule(int UserID, int ModuleID)
    {

    }
    private void loadDataAccess()
    {
        SqlConnection sqlConnec = ConnectData.getConnection();
        objHocVienLV2 = new DAHocVienLV2(sqlConnec);
        objHocVienLV3LV5 = new DAHocVienLV3LV5(sqlConnec);
        objHocVienLV6LV8 = new DAHocVienLV6LV8(sqlConnec);
        objHocVien = new DAHocVien(sqlConnec);
        objNguoidung = new DANguoidung(sqlConnec);
        objTrangThaiGoi = new DATrangThaiGoi(sqlConnec);
        objTrangThai = new DATrangThai(sqlConnec);
        objLevel = new DALevel(sqlConnec);
        objQuyen = new DAQuyen(sqlConnec);
        objLog = new DALog(sqlConnec);
    }
    //Main load
    protected void Page_Load(object sender, EventArgs e)
    {
        loadDataAccess();
        intUserID = Convert.ToInt32(Session["UserID"].ToString());
        if (Request.QueryString["ChucNang"] != null)
        {
            bool boolCheck = StringUntil.checkNumber(Request.QueryString["ChucNang"].Trim());
            if (boolCheck)
            {
                intChucNangID = Convert.ToInt32(Request.QueryString["ChucNang"].Trim());
            }
        }
    }

    //Su kien bin from NguoiTimNguon
    protected void grvHocVien_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.DataItem != null)
            {
                e.Row.Attributes["onmouseover"] = "javascript:setMouseOverColor(this);";
                e.Row.Attributes["onmouseout"] = "javascript:setMouseOutColor(this);";
                // Modify     
                Label HocVienIDTemp = ((Label)e.Row.FindControl("HocVienID"));
                Label LevelIDTemp = ((Label)e.Row.FindControl("LevelID"));
                
                Label lblMaHocVienGVTemp = ((Label)e.Row.FindControl("lblMaHocVienGV"));    
                HyperLink hplAddGVTemp = ((HyperLink)e.Row.FindControl("hplAddGV"));
                if (lblMaHocVienGVTemp != null)
                {
                    string strViewMaHocVien = "JavaScript:OpenWinCenter(700, 800, '" + lblMaHocVienGVTemp.Text + "', 'EventHocVien.aspx?LevelView=" + LevelIDTemp.Text + "&HocVienID=" + HocVienIDTemp.Text + "&EventView=True');";
                    e.Row.Cells[2].Attributes["Ondblclick"] = strViewMaHocVien;
                    e.Row.Cells[3].Attributes["Ondblclick"] = strViewMaHocVien;
                    e.Row.Cells[4].Attributes["Ondblclick"] = strViewMaHocVien;
                    e.Row.Cells[5].Attributes["Ondblclick"] = strViewMaHocVien;
                    e.Row.Cells[6].Attributes["Ondblclick"] = strViewMaHocVien;
                    e.Row.Cells[7].Attributes["Ondblclick"] = strViewMaHocVien;
                    e.Row.Cells[8].Attributes["Ondblclick"] = strViewMaHocVien;
                    e.Row.Cells[9].Attributes["Ondblclick"] = strViewMaHocVien;
                    //DataSet drQUyenTemp = objNguoidung.loadQuyenNguoidungbyNguoiDung(intUserID);
                    //foreach (DataRow drTemp in drQUyenTemp.Tables[0].Rows)
                    //{
                    //    int intQuyenTemp = Convert.ToInt32(drTemp["QuyenID"].ToString());
                    //    if (intQuyenTemp == 3 && LevelIDTemp.Text.Equals("2"))
                    //    {
                    //        boolLevel2345678 = false;
                    //        break;
                    //    }
                    //}
                    hplAddGVTemp.Attributes.Add("OnClick", "JavaScript:OpenWinCenter(700, 800, '', 'EventHocVienUpdate.aspx?LevelView=" + LevelIDTemp.Text + "&UserList=" + intUserList.ToString() + "&HocVienID=" + HocVienIDTemp.Text + "&EventCreate=True');");
                }
                Label lblNoidungGVTemp = ((Label)e.Row.FindControl("lblNoidungGV"));
               // Label lblNgayGoiGanNhatGVTemp = ((Label)e.Row.FindControl("lblNgayGoiGanNhatGV"));
                if (lblNoidungGVTemp != null)
                {
                    try
                    {
                        int intHocVienID = Convert.ToInt32(HocVienIDTemp.Text);
                        int intLevelID = Convert.ToInt32(LevelIDTemp.Text);
                        DataRow dr = null;
                        if (intLevelID >= 2 && intLevelID <= 5)
                        {
                            try
                            {
                                dr = objHocVienLV3LV5.loadHocVienLV3LV5ID(intHocVienID, intLevelID +1);
                                if (dr["NoiDungNgayGoi"].ToString() == "")
                                {
                                    dr = objHocVienLV2.loadHocVienLV2ID(intHocVienID, intLevelID);
                                }
                            }
                            catch
                            {
                                dr = objHocVienLV3LV5.loadHocVienLV3LV5ID(intHocVienID, intLevelID);
                            }
                            
                        }
                        else if (intLevelID == 1)
                        {
                            try
                            {
                                dr = objHocVienLV2.loadHocVienLV2ID(intHocVienID, intLevelID +1);
                            }
                            catch
                            {
                                dr = objHocVienLV2.loadHocVienLV2ID(intHocVienID, intLevelID);
                            }                            
                        }  
                        else if (intLevelID > 6 && intLevelID < 10)
                        {
                            try
                            {
                                dr = objHocVienLV6LV8.loadHocVienLV6LV8ID(intHocVienID, intLevelID + 1);           
                            }
                            catch
                            {
                                dr = objHocVienLV6LV8.loadHocVienLV6LV8ID(intHocVienID, intLevelID);             
                            }
                        }
                        if (dr != null)
                        {
                            if (!dr["NoiDungTaiSao"].ToString().Trim().Equals(""))
                            {
                                lblNoidungGVTemp.Text = dr["NoiDungTaiSao"].ToString();
                            }
                            else
                            {
                                lblNoidungGVTemp.Text = dr["NoiDungNgayGoi"].ToString();
                            }
                        }
                        else
                        {
                            lblNoidungGVTemp.Text = "";
                        }
                    }
                    catch
                    {
                        lblNoidungGVTemp.Text = "";
                    }
                }
            }
        }
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        loadTimDanhSachHocVien();
    }
    protected void grvHocVien_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.grvHocVien.EditIndex = -1;
        this.grvHocVien.PageIndex = e.NewPageIndex;
        loadTimDanhSachHocVien();
    }
    protected void rblTrangThaiGoi_SelectedIndexChanged(object sender, EventArgs e)
    {
        loadTimDanhSachHocVien();
    }
    protected void grvHocVien_Sorting(object sender, GridViewSortEventArgs e)
    {
        if (ViewState["SortOrder"] == null)
        {
            ViewState["SortOrder"] = " ASC";
        }
        else if (ViewState["SortOrder"].ToString() == " ASC")
        {
            ViewState["SortOrder"] = " DESC";
        }
        else
        {
            ViewState["SortOrder"] = " ASC";
        }
        ViewState["sortExpr"] = e.SortExpression + ViewState["SortOrder"];
        loadTimDanhSachHocVien();
    }
#endregion    
}
